package com.simpou.commons.utils.file;

import com.simpou.commons.utils.file.FileHelper;
import com.simpou.commons.utils.file.PropertyHelper;
import com.simpou.commons.utils.string.Strings;
import com.simpou.commons.utils.tests.TestRunner;
import com.simpou.commons.utils.tests.annot.ClassTest;
import com.simpou.commons.utils.tests.annot.IgnoreValidation;
import com.simpou.commons.utils.tests.annot.TestOrder;

import org.junit.AfterClass;
import static org.junit.Assert.*;

import org.junit.BeforeClass;
import org.junit.Test;

import org.junit.runner.RunWith;

import java.io.FileNotFoundException;
import java.io.IOException;

import java.util.MissingResourceException;


@IgnoreValidation
@RunWith(TestRunner.class)
@ClassTest(PropertyHelper.class)
public class PropertyHelperITCase {
    private static PropertyHelper propertyHelper;
    private static String key;
    private static String key2;
    private static String param;
    private static String value;
    private static String value2;
    private static String filePath;
    private static String content;

    @BeforeClass
    public static void setUpClass() throws Exception {
        filePath = PropertyHelper.getTempDir() + "teste.txt"; //UUID.randomUUID();
        key = "prop1";
        key2 = "prop2";
        param = "param";
        value = "value {0}";
        value2 = "value2";
        content = key + "=" + value;
        FileHelper.write(filePath, content, false, false);
        propertyHelper = new PropertyHelper(filePath);
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
        propertyHelper.close();
        FileHelper.delete(filePath);
        assertFalse(FileHelper.exists(filePath));
    }

    @Test
    @TestOrder(1)
    public void testSetProperty_String_int() throws Exception {
        String prop;

        prop = propertyHelper.setProperty(key2, 0);
        assertTrue(prop.startsWith(PropertyHelper.DEF_VALUE_NOT_SETTED_PREFIX));

        prop = propertyHelper.setProperty(key2, 2);
        assertTrue(prop.startsWith(PropertyHelper.DEF_VALUE_NOT_SETTED_PREFIX));
        assertTrue(prop.contains("{0}"));
        assertTrue(prop.contains("{1}"));
    }

    @Test
    @TestOrder(2)
    public void testSetProperty_String_String()
        throws FileNotFoundException, IOException {
        propertyHelper.setProperty(key2, value2);

        String text = FileHelper.read(filePath);
        String props = key2 + "=" + value2 + "\n" + content;
        assertEquals(props,
            text.substring(text.indexOf("\n") + 1, text.length()));
    }

    @Test
    @TestOrder(3)
    public void testGetProperty() throws Exception {
        String prop = propertyHelper.getProperty(key, param);
        assertEquals(Strings.replaceParams(value, param), prop);

        prop = propertyHelper.getProperty(key2);
        assertEquals(value2, prop);

        try {
            prop = propertyHelper.getProperty("key_error");
            fail();
        } catch (MissingResourceException ex) {
        }
    }
}
